defmodule TestDeeprBehaviour do
    @moduledoc false

    defmacro __using__(opts) do
        quote location: :keep,
              bind_quoted: [
                  user_mod: opts[:user_mod],
                  project_type: opts[:project_type]
              ] do
            @behaviour TestDeeprBehaviour
            import Util


            def neo4j_list(obj, user, select, where \\ %{}, match \\ [], optional \\ [], attach \\ false, page \\ 1) do
                args =
                    %{
                        obj => %{
                            "[]" => page,
                            "total_page" => true,
                            "attach" => attach,
                            "match" => match,
                            "optional" => optional,
                            "=>" => select,
                            "()" => where,
                        }
                    }
                map = %{
                    "args" => Jason.encode!(args),
                    "token" => UserLib.get_token(user)
                }
                __MODULE__.list(map)
            end

            def psql_list(obj, select, where, page \\ 1) do
                user = unquote(user_mod).login(unquote(user_mod).phone(), unquote(user_mod).passwd())
                args =
                    %{
                        obj => %{
                            "[]" => page,
                            "total_page" => true,
                            "=>" => select,
                            "()" => where,
                        }
                    }
                map = %{
                    "args" => Jason.encode!(args),
                    "token" => UserLib.get_token(user)
                }
                list(map)
            end


            def psql_info(obj, select, where) do
                user = unquote(user_mod).login(unquote(user_mod).phone(), unquote(user_mod).passwd())
                args =
                    %{
                        obj => %{
                            "=>" => select,
                            "()" => where,
                        }
                    }
                map = %{
                    "args" => Jason.encode!(args),
                    "token" => UserLib.get_token(user)
                }
                info(map)
            end

            def list(map) do
                url = "#{unquote(project_type)}/deepr/#{get_function()}"
                TestHttpRequest.send(:post, url, map)
            end

            def info(map) do
                url = "#{unquote(project_type)}/deepr/#{get_function()}"
                TestHttpRequest.send(:post, url, map)
            end

            def insert(map) do
                url = "#{unquote(project_type)}/deepr/#{get_function()}"
                TestHttpRequest.send(:post, url, map)
            end

            def update(map) do
                url = "#{unquote(project_type)}/deepr/#{get_function()}"
                TestHttpRequest.send(:post, url, map)
            end

            def delete(map) do
                url = "#{unquote(project_type)}/deepr/#{get_function()}"
                TestHttpRequest.send(:post, url, map)
            end

            def info(obj, select, where \\ %{}, match \\ [], optional \\ []) do
                user = unquote(user_mod).login(unquote(user_mod).phone(), unquote(user_mod).passwd())
                args = %{
                    obj => %{
                        "()" => where,
                        "=>" => select,
                        "match" => match,
                        "optional" => optional,
                    },
                }
                map = %{
                    "args" => Jason.encode!(args),
                    "token" => UserLib.get_token(user)
                }
                url = "#{unquote(project_type)}/deepr/#{get_function()}"
                TestHttpRequest.send(:post, url, map)
            end

        end
    end

    def random_one(list, obj) do
        Kernel.get_in(list, ["data", obj, "list"])
        |> Random.random_one(%{})
    end

    def random_list(list, obj, num \\ 2) do
        Kernel.get_in(list, ["data", obj, "list"])
        |> Random.random_list(num)
    end

    def deepr_obj(mod) do
        Macro.underscore(mod)
        |> String.replace("_test", "")
    end

    def user_obj() do
        "user"
    end


end
